const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            childDataForm1: [{}],
            dataForm1: {},
            dataForm2: { Contractor_Type: '0' },
            category: [],
            village: [],
            cityValue: [],
            organization: [],
            personal_information: [],
            natural_resources_quality_grade: [],
            natural_resources_contract_type:[],
            village_collective: [],
            Natural_Resource_Tag: [],
            Natural_Resource_Usage: [],
            Natural_Resource_Usage1: [],
            Usage_status: [{ label: '空闲', value: '0' }, { label: '占用', value: '1' }, { label: '租用', value: '2' }],
            Contractor_Type: [{ label: '分配', value: '0' }, { label: '租赁', value: '1' }],
            is: true,
            dataForm2:{
                Contract_Type:'0'
            },
            Ownership_Rights: {
                10: '国有土地所有权',
                20: '国有土地使用权',
                30: '集体土地所有权'
            },
        };
    },
    watch: {
        Usage(val) {
            if (this.action == 'Modify') {
                this.cityValue = val;
            }
        },
        'dataForm.Natural_Resource_Usage_SerialNumber'(val) {
            if (this.action == 'Modify' && this.is) {
                this.is = false;
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'natural_resources_usage',
                        Condition: "Row_Status = '0'"
                    },
                    success: (data) => {
                        let obj = data.filter(i => i.Natural_Resource_Usage_SerialNumber == val)[0];
                        let obj1 = data.filter(i => i.Natural_Resource_Usage_SerialNumber == obj.Parent_SerialNumber)[0];
                        this.cityValue = [obj1.Natural_Resource_Usage_SerialNumber, val]
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
                this.dataForm1.Natural_Resource_SerialNumber = this.nodeKey;
                this.dataForm1.Natural_Resource_Ownership = this.dataForm.Village_Collective_SerialNumber;
                if (this.dataForm.Natural_Resource_Ownership_Rights == '30') {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableName`,
                        type: "POST",
                        data: {
                            Data: 'village_collective',
                            Condition: "Row_Status='0' AND VillageNumber='" + this.dataForm.Village_SerialNumber + "'",
                        },
                        success: (data) => {
                            this.village_collective = data; // 直接使用 this
                            this.handlechange(this.dataForm.Village_Collective_SerialNumber)
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                } else {
                    this.handlechange1(this.dataForm.Village_Collective_SerialNumber);
                }
            }
        }
    },
    mounted() {
        this.getCategory();
    },
    methods: {
        count_End_Date(val){
            if (!isNaN(this.dataForm2.Expiration_Date)&&this.dataForm2.Expiration_Date&&this.dataForm2.Start_Date) {
                let End_Date = getYMDDate(new Date(this.dataForm2.Start_Date).setFullYear(new Date(this.dataForm2.Start_Date).getFullYear() + parseInt(this.dataForm2.Expiration_Date))  );
                this.dataForm2.End_Date = getYMDDate(new Date(End_Date).setDate(new Date(End_Date).getDate() - 1)); 
            }
        },
        changeVillage(val) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'village_collective',
                    Condition: "Row_Status='0' AND VillageNumber='" + val + "'"
                },
                success: (data) => {
                    this.village_Collective = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.category = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_contract_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.natural_resources_contract_type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'natural_resources_usage',
                    NodeCode: '',
                },
                success: (data) => {
                    data.forEach(item => {
                        if (item.children) {
                            item.children.forEach(it => {
                                it.Natural_Resource_Usage_Name = it.Natural_Resource_Usage_Name + (it.Natural_Resource_Type ? ' - ' + it.Natural_Resource_Type : '')
                            })
                        }
                    })
                    this.Natural_Resource_Usage = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_usage',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Usage1 = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_tags',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Tag = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.personal_information = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'base_system_village',
                    Condition: "TownNumber LIKE '232723%'",
                },
                success: (data) => {
                    this.village = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_quality_grade',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.natural_resources_quality_grade = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'government_agencies',
                    NodeCode: "",
                },
                success: (data) => {
                    this.organization = data; // 直接使用 this
                    if (this.dataForm.Natural_Resource_Ownership_Rights != '30') { 
                        this.handlechange1(this.dataForm.Village_Collective_SerialNumber)
                    }
                  
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handlechange(val){
            if (val) {
                let obj = this.village_collective.filter(i=>i.Village_Collective_SerialNumber == val)[0];
                if (obj) {
                    this.dataForm2.Contractor_Name = obj.Org_Name;
                }
            }
        },
        organization1(arr , val){
            let str = '';
            for (let i = 0; i < arr.length; i++) {
                const item = arr[i];
                if (item.Government_Agencies_SerialNumber == val) {
                    str = item.Agensies_Name;
                    break;
                } else if (item.children && item.children.length > 0) {
                    str = this.organization1(item.children,val);
                    if (str) {
                        break;
                    }
                }
            }
            return str;
        },
        handlechange1(val){
            if (val) {         
                let str = this.organization1(this.organization , val[val.length - 1]) 
                if (str) {
                    this.dataForm2.Contractor_Name  = str;
                }
                
            }
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;

                //校验是否与已有的业务数据冲突
               
                let dDateFrom = this.dataForm2.Start_Date
                let dDateEnd = this.dataForm2.End_Date

                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'natural_resources_contract',
                        Condition: "  Contract_Type ='0' and Row_Status ='0' and Start_Date <='" + dDateEnd + "' AND End_Date >= '" + dDateFrom + "'" +
                            " and Natural_Resource_SerialNumber='" + this.dataForm.Natural_Resource_SerialNumber + "'"
                    },
                    success: (data) => {

                        if (data.length == 0) {



                            this.dataForm.Village_Collective_SerialNumber = this.dataForm1.Natural_Resource_Ownership;
                            this.dataForm2.Natural_Resource_SerialNumber = this.dataForm.Natural_Resource_SerialNumber;
                            let lJSONObject_DataSet = {
                                Add: {
                                    natural_resources_allocation: [this.dataForm1], natural_resources_contract: [this.dataForm2]
                                },
                                Modify: {
                                    natural_resources_management: [this.dataForm]
                                }
                            }
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            if (!lJSONObject_DataSet['Add']) {
                                lJSONObject_DataSet['Add'] = new Object();
                            }
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSave",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet, (res) => {
                                if (res.Status == 'success') {
                                    window.location.href = './index.html'
                                }
                                this.loading = false;
                            })
                        }
                        else {
                            this.$message({
                                message: '该合同签署期间与已有合同冲突,请检验！',
                                type: 'warning'
                            });

                        }

                    },
                    complete: (XMLHttpRequest) => {
                        this.loading = false;
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });

                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalSave`,
                //     method: "post",
                //     data: {
                //         Data: JSON.stringify(lJSONObject_DataSet),
                //     },
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             window.location.href = './index.html'
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         this.loading = false;
                //         requestPretreatment(XMLHttpRequest);
                //     }

                // });
            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }

        },
        changeCity(val) {
            this.dataForm.Natural_Resource_Usage_SerialNumber = val[val.length - 1];
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader);
app.component('yl-select', ylSelect);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
